<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/29
 * Time: 13:04
 */

namespace app\index\controller;


use think\Controller;

/**
 * Class Cart
 * @package app\index\controller
 * 购物车控制器
 */
class Cart extends Controller
{
    //主页
    public function index(){
        //处理title
        $conf = ['title'=>'阿里巴巴1688.com - 购物车','css'=>'cart','js'=>'cart'];
        $this->assign('conf',$conf);
        $cart = session('cart');
        $this->assign('cart',$cart);
        //视图
        return view();
    }
    //更新购物车(数量增加、减少、删除)
    public function updateCart()
    {
        //1.获取数量和键名
        $num = input('post.num');
        $kk = input('post.kk');
        //4.删除
        if (!$num) {
            $data = [
                'sid' => $kk,// 唯一 sid，添加购物车时自动生成
                'num' =>'',
            ];
            (new \cart\Cart())->update($data);
        } else {
            //2.执行修改购物车
            $data = [
                'sid' => $kk,// 唯一 sid，添加购物车时自动生成
                'num' => $num
            ];
            (new \cart\Cart())->update($data);
            //3.组一个数组 返回给前台
            $cart = session('cart');
            $ajaxRuturn = [
                'total' => $cart['goods'][$kk]['total'],
                'total_price' => $cart['total_price']
            ];
            echo json_encode($ajaxRuturn);
        }
    }

}